
import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:massagist/manager/config.dart';
import 'package:massagist/manager/request.dart';
import 'package:massagist/pages/phone_login_page.dart';
import 'package:massagist/pages/privacy_page.dart';
import 'package:massagist/pages/tab_page.dart';
import 'package:shanyan/shanYanResult.dart';
import 'package:shanyan/shanyan.dart';

import '../generated/l10n.dart';

class PhoneAutoPage extends StatefulWidget {
  @override
  State<PhoneAutoPage> createState() => PhoneAutoPageState();
}

class PhoneAutoPageState extends State<PhoneAutoPage> {
  final OneKeyLoginManager oneKeyLoginManager = new OneKeyLoginManager();
  String cendToken = "";


  Future<void> initPlatformState() async {
    String appId = "LUhGK2JE";
    oneKeyLoginManager.init(appId: appId).then((shanYanResult) {
      int _code = (shanYanResult.code ?? 0);
      String _result = shanYanResult.message ?? "";
      String _content = shanYanResult.toJson().toString();

      print ("code = " + _code.toString());
      print ("_result = " + _result);
      print ("_content = " + _content);

      if (1000 == shanYanResult.code) {
        //初始化成功
        getPhoneInfo();
      } else {
        //初始化失败
      }
    });

    oneKeyLoginManager.setDebug(true);
    oneKeyLoginManager
        .getOperatorType()
        .then((value) => print("getOperatorType===" + value));
    oneKeyLoginManager
        .getOperatorInfo()
        .then((value) => print("getOperatorInfo===" + value));
    oneKeyLoginManager
        .setPrivacyOnClickListener((PrivacyOnClickEvent privacyOnclickEvent) {
      Map map = privacyOnclickEvent.toMap();
      print("setPrivacyOnClickListener===" + map.toString());
    });
  }

  void getPhoneInfo() {
    oneKeyLoginManager.getPhoneInfo().then((value) {
      print (" ================= ");
      print (value.code.toString());
      print (value.message.toString());
      print (value.toJson().toString());

      cendToken = value.toJson().toString();
    });

    // oneKeyLoginManager.openLoginAuth().then((value) {
    //   print (" ================= ");
    //   print (value.code.toString());
    //   print (value.message.toString());
    //   print (value.toJson().toString());
    // });
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    initPlatformState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            GestureDetector(
              child: Container(
                margin: EdgeInsets.only(top: 59.sp, left: 17.sp),
                child: Icon(Icons.close),
              ),
            ),

            Container(
              child: Column(
                children: [

                  Container(
                    alignment: Alignment.center,
                    margin: EdgeInsets.only(top: 67),
                    child: ClipRRect(
                      borderRadius: BorderRadius.circular(50.sp),
                      child: Image.network(
                        "https://img0.baidu.com/it/u=3575652686,492296591&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500",
                        width: 100.sp,
                        height: 100.sp,
                        fit: BoxFit.cover,
                      ),
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(top: 40.sp),
                    child: Text("188****7777", style: TextStyle(
                      color: Colors.black,
                      fontWeight: FontWeight.bold,
                      fontSize: 22.sp,
                    )),
                  ),
                  GestureDetector(
                    child: Container(
                      width: double.infinity,
                      height: 44.sp,
                      margin: EdgeInsets.only(left: 32.sp, right: 32.sp, top: 40.sp),
                      decoration: BoxDecoration(
                        color: Color(0xFF00CA7E),
                        borderRadius: BorderRadius.circular(22.sp), // 设置圆角半径
                      ),
                      child: Center(
                        child: Text(
                          S.current.ONE_BUTTON_LOGIN,
                          style: TextStyle(color: Colors.white, fontSize: 16.sp),
                        ),
                      ),
                    ),
                    onTap: () async {
                      // Navigator.pushAndRemoveUntil( context, MaterialPageRoute(builder: (context) => TabPage()), (route) => false, );
                      // initPlatformState();
                      print ("login");

                      var result = await Request.requestAutoPhone(cendToken);
                      if (result.code == RESPONSE_SUCCESS) {
                        Navigator.pushAndRemoveUntil( context, MaterialPageRoute(builder: (context) => TabPage()), (route) => false, );
                      } else {
                        ScaffoldMessenger.of(context).showSnackBar(
                          SnackBar(content: Text(result.msg)),
                        );
                      }
                    },
                  ),
                  GestureDetector(
                    child: Container(
                      margin: EdgeInsets.only(top: 20.sp),
                      child: Text(S.current.OTHER_PHONE_LOGIN, style: TextStyle(
                        color: Color(0xFF333333),
                        fontSize: 12.sp,
                      )),
                    ),
                    onTap: () {
                      Navigator.push(context, MaterialPageRoute(builder: (context) => PhoneLoginPage()));
                    },
                  )
                ],
              ),
            ),
            Container(
              margin: EdgeInsets.only(top: 40.sp),
              alignment: Alignment.center,
              child: RichText(
                text: TextSpan(
                  style: TextStyle(
                    fontSize: 12.sp,
                    color: Color(0xFF999999),
                  ),
                  children: [
                    TextSpan(text: S.current.LOGIN_RIGHT_AGREE),
                    TextSpan(
                      text: S.current.LOGIN_RIGHT_PRIVACY,
                      style: TextStyle(
                        color: Color(0xFF00CA7E),
                      ),
                      recognizer: TapGestureRecognizer()..onTap = () {
                        Navigator.push(context, MaterialPageRoute(builder: (context) => PrivacyPage()));
                      }
                    )
                  ],
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}